﻿<cfscript>
/** 
* DataBase Frameworks - Sequence
* 
* @hint "DataBase Frameworks - Sequence - CommonTask" 
*/ 

component 
	displayname="public.service.db.senate.sequence.CommonTask" 
	output=false 
	accessors=true 
	{

	property type="string" name="TermID" hint="学期代码";
	property type="string" name="CourseID" hint="课程ID";
	property type="string" name="courseIDX" hint="课序号";

	/**
	* @hint "初始化对象"
	*/
	public function init() output=false {
		return this;
	}

	/**
	* @hint "生成通选课教学任务的课序号"
	*/
	public string function getIDX() output=false {
		/* 
			课序号规则 3位代码
		*/
		var sql = "	SELECT
						cat_id 
					FROM
						t_course 
					WHERE
						cid = :courseId ";
		var queryObj = new Query( datasource = application.dnsMaster );
		queryObj.addParam( name="courseId", value=getCourseId(), cfsqltype="cf_sql_varchar" );		
		var rs_result = queryObj.execute( sql=sql ).getResult();
		
		var nextID = left(rs_result.cat_id, 1) & numberFormat(1, "0_");
		
		sql = "	SELECT
					  cidx
				  FROM
					  t_common_task 
				  WHERE
					  cid = :courseId 
					  AND
					  term_id = :termId 
				  ORDER BY
					  cidx DESC ";
		queryObj = new Query( datasource=application.dnsMaster, maxRows=1 );
		queryObj.addParam( name="termId", value=getTermID(), cfsqltype="cf_sql_char" );
		queryObj.addParam( name="courseId", value=getCourseId(), cfsqltype="cf_sql_varchar" );		
		var rs_check = queryObj.execute( sql=sql ).getResult();

		if (rs_check.recordCount) {
			/* 创建一个追加id */
			nextID = left(rs_result.cat_id, 1) & numberFormat((right(rs_check.cidx,2) + 1), "0_");
		}

		return nextID;
	}
	
	/**
	* @hint "自定义主键规则"
	*/
	public string function getID() output=false {
		
		
		var nextID = getTermID() & "," & getCourseID() & "," & getcourseIDX();

		/* 
			主键生成规则 
			
			培养方案号18位 3位学期代码 , 6位课程号 , 3位课序号

			Unique: TERM_ID, CID, CIDX
		*/
		return nextID;
	}

}

</cfscript>